//
//  DrawerViewController.m
//  ImitateZH
//
//  Created by lanou3g on 16/3/1.
//  Copyright © 2016年 Highyoung. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    //添加手势
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        self.tap.delegate = self;
        [self.view addGestureRecognizer:self.tap];
    }
    
    
}
-(void) tapAction {
    // 手势只是用来关闭抽屉的
    [self showRootViewController:nil];
}



// 设置根控制器
- (void)setRootVCC:(UIViewController *) viewcontroller{
    if (viewcontroller) {
        
    
    [self.rootVC.view removeFromSuperview];
    self.rootVC = viewcontroller;
    [self addChildViewController:viewcontroller];
    // 如果手势开着 证明抽屉开着
    if (self.tap.enabled) {
        viewcontroller.view.frame = CGRectMake(200, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        
    }
    
    [self.view addSubview:viewcontroller.view];
    
    
    [self showRootViewController:nil];
    // 设置导航栏中的按钮
    [self setNaButton];
        
    }
    
}
-(void)setNaButton{
    UINavigationController *n = (UINavigationController *)self.rootVC;
    UIViewController *VC = n.viewControllers[0];
    
    VC.navigationItem.leftBarButtonItem  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Home_Icon"] style:(UIBarButtonItemStyleDone) target:self action:@selector(openAction)];
 
}
-(void)openAction{
    [self showleftViewController:nil];
}



// 打开抽屉
-(void)showleftViewController:(UIViewController *)viewcontroller{
    if (![self.view.subviews containsObject:self.leftVC.view]) {
        //插入leftview
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    // 动画移动视图控制器
    [UIView animateWithDuration:0.5 animations:^{
        self.rootVC.view.frame = CGRectMake(200, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }];
    
    // 打开手势
    [self.tap setEnabled:YES];
    // 关闭主屏幕的交互
    self.rootVC.view.userInteractionEnabled  = NO;
    
    
}


// 展示中间视图方法（关闭抽屉）
-(void)showRootViewController:(UIViewController *)viewController{
    // 动画移动视图控制器
    [UIView animateWithDuration:0.5 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }];
    
    // 关闭手势
    [self.tap setEnabled:NO];
    // 打开主屏幕的交互
    self.rootVC.view.userInteractionEnabled  = YES;

}


// delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.tap) {
        return  CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:_rootVC.view]);
        
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
